"use strict";
cc._RF.push(module, '17616jLtnJJQIm7/nlO0iZk', 'user_bindBank_pop');
// scripts/user/user_bindBank_pop.js

"use strict";

// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/*
 *绑定银行卡界面
*/
cc.Class({
    extends: cc.Component,

    properties: {
        edCardNumber: cc.EditBox, //银行卡号
        labOpenBank: cc.Label, //开户银行
        edCode: cc.EditBox, //验证码
        labDec: cc.Label, //获取验证码（倒计时） 
        _bgetVerity: true,
        callback: null,
        bankContent: null,
        intervalTime: 60,
        bankCardNum: "",
        verify: ""
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start: function start() {},


    init: function init(data) {
        this.callback = data;
    },

    onTextChangedCard: function onTextChangedCard(text, editbox, customEventData) {
        var txt = text;
        if (Utils.isInt(txt) || txt == "") this.bankCardNum = txt;else {
            txt = this.bankCardNum;
            editbox.string = txt;
            return;
        }

        if (txt.length >= 15) {
            var recv = function recv(err, bin) {
                if (err == null) {
                    if (bin != "") {
                        if (bin.cardType == "DC") {
                            this.labOpenBank.string = bin.bankName;
                        } else {
                            ComponentsUtils.showTips("目前只支持绑定储蓄卡！");
                        }
                    }
                } else {
                    this.labOpenBank.string = "";
                }
            };
            LotteryUtils.getBankBin(txt, recv.bind(this));
        } else {
            this.labOpenBank.string = "";
        }
    },

    //获取验证码
    onGetVerifyCode: function onGetVerifyCode() {
        if (!this._bgetVerity) return;
        if (this.edCardNumber.string != "") {
            if (this.labOpenBank.string == "") {
                ComponentsUtils.showTips("请输入有效银行卡号");
                return;
            }
        } else {
            ComponentsUtils.showTips("银行卡不能为空！");
            return;
        }
        var self = this;
        var recv = function (ret) {
            ComponentsUtils.unblock();
            if (ret.Code !== 0) {
                cc.error(ret.Msg);
                ComponentsUtils.showTips(ret.Msg);
            } else {
                self._bgetVerity = false;
                ComponentsUtils.showTips("发送成功!");
                self.schedule(self._updateVerify, 1);
            }
        }.bind(this);

        var data = {
            Equipment: Utils.getEquipment(),
            Token: User.getLoginToken(),
            Mobile: User.getTel(),
            VerifyType: DEFINE.SHORT_MESSAGE.BINDBANK
        };
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETVERIFY, data, recv.bind(this), "POST");
        ComponentsUtils.block();
    },

    _updateVerify: function _updateVerify() {
        this.intervalTime = this.intervalTime - 1;
        this.labDec.string = this.intervalTime + "秒后重发";
        if (this.intervalTime <= 0) {
            this._bgetVerity = true;
            this.unschedule(this._updateVerify);
            this.intervalTime = 60;
            this.labDec.string = "获取验证码";
        }
    },

    checkInput: function checkInput() {
        if (this.edCardNumber.string != "") {
            if (this.labOpenBank.string == "") {
                ComponentsUtils.showTips("请输入有效银行卡号");
                return;
            }
        } else {
            ComponentsUtils.showTips("银行卡不能为空！");
            return false;
        }

        if (this.edCode.string != "") {
            if (!Utils.isVerify(this.edCode.string)) {
                ComponentsUtils.showTips("验证码格式不对！");
                return false;
            }
        } else {
            ComponentsUtils.showTips("验证码不能为空！");
            return false;
        }
        return true;
    },

    //绑定
    onTrue: function onTrue() {
        if (this.labOpenBank.string == "") {
            ComponentsUtils.showTips("请输入有效银行卡号");
            return;
        }
        if (this.checkInput()) {
            var recv = function (ret) {
                ComponentsUtils.unblock();
                if (ret.Code == 0) {
                    ComponentsUtils.showTips("绑定银行卡成功！");
                    this.callback("");
                    this.onClose();
                } else {
                    ComponentsUtils.showTips(ret.Msg);
                }
            }.bind(this);

            var data = {
                Token: User.getLoginToken(),
                UserCode: User.getUserCode(),
                BankName: this.labOpenBank.string,
                CardNum: this.edCardNumber.string,
                Area: "",
                Mobile: User.getTel(),
                VerifyCode: this.edCode.string
            };
            CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.BINDBANKCARD, data, recv.bind(this), "POST");
            ComponentsUtils.block();
        }
    },

    onClose: function onClose() {
        this.node.getComponent("Page").backAndRemove();
    }

    // update (dt) {},
});

cc._RF.pop();